package com.smbms.providers.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smbms.commons.bean.Providers;
import com.smbms.commons.config.JsonResult;
import com.smbms.commons.config.ResultCode;
import com.smbms.commons.config.ResultTool;
import com.smbms.commons.service.ProvidersService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/provider")
public class ProvidersController {
    @Resource
    private ProvidersService providersService;


    @GetMapping("/find")
    public JsonResult<PageInfo<Providers>> findAllProviders(@RequestParam(name = "page",defaultValue = "1") Integer page){
        PageHelper.startPage(page,2);
        List<Providers> list = providersService.list();
        PageInfo<Providers> pageInfo=new PageInfo<>(list);
        return ResultTool.success(pageInfo);
    }

    @PostMapping("/save")
    public JsonResult<Providers> save(Providers provider){
        boolean b = providersService.save(provider);
        if(!b){
            return ResultTool.fail(ResultCode.COMMON_FAIL);
        }
        return ResultTool.success();
    }

    @PutMapping("/update")
    public JsonResult<Providers> update(Providers provider){
        boolean b = providersService.saveOrUpdate(provider);
        if(!b){
            return ResultTool.fail(ResultCode.COMMON_FAIL);
        }
        return ResultTool.success();
    }

    @DeleteMapping("/remove")
    public JsonResult<Providers> remove(Integer id){
        boolean b = providersService.removeById(id);
        if(!b){
            return ResultTool.fail(ResultCode.COMMON_FAIL);
        }
        return ResultTool.success();
    }


}
